package com.ruix.app.ui.stratum.setting;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.constant.RuiXConstant;
import com.ruix.app.manager.UserManager;
import com.ruix.app.mapper.ClasssDetailMapper;
import com.ruix.app.model.ClassDetail;
import com.ruix.app.model.Classes;
import com.ruix.app.model.WebParcel;
import com.ruix.app.network.ApiConstant;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.CommonWebActivity;
import com.ruix.app.ui.base.BaseActivity;
import com.ruix.app.ui.stratum.status.ClassStatusActivity;
import com.ruix.app.util.RoleUtil;
import com.ruix.app.widget.SwitchButton;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/4/21.
 */

public class ClassSettingActivity extends BaseActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener {


    public static Intent newIntent(Context context, Classes classes,ClassDetail classDetail){
        Intent intent = new Intent(context,ClassSettingActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("classes",classes);
        bundle.putSerializable("classDetail",classDetail);
        intent.putExtras(bundle);
        return intent;
    }

    private Classes currentClasses;
    private ClassDetail classDetail;
    private SwitchButton shareSwitch;
    private TextView detaiTextView,menberTextView;

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_class_setting;
    }

    @Override
    protected void initView() {
        setTitle("圈子设置");
        shareSwitch = (SwitchButton) findViewById(R.id.sb_need_comfirm);
        detaiTextView = (TextView) findViewById(R.id.tv_class_detail);
        menberTextView = (TextView) findViewById(R.id.tv_class_menber);
    }

    private void updateView(){
        if (RoleUtil.isAdviserOrHelperOrTeacher(classDetail.getRole())){
            findViewById(R.id.btn_feedback_content).setVisibility(View.VISIBLE);
            findViewById(R.id.btn_push_advice).setVisibility(View.GONE);
            findViewById(R.id.rl_share_able).setVisibility(View.VISIBLE);
            if (classDetail != null) {
                shareSwitch.setChecked(classDetail.isCanSharePost(), false);
            }
        } else {
            findViewById(R.id.btn_feedback_content).setVisibility(View.GONE);
            findViewById(R.id.btn_push_advice).setVisibility(View.VISIBLE);
            findViewById(R.id.rl_share_able).setVisibility(View.GONE);
        }

        //师友圈
        if (currentClasses.isTeacherFriendCircle()){
            findViewById(R.id.btn_notification_view).setVisibility(View.VISIBLE);//录取通知书
            detaiTextView.setText("圈子详情");
            menberTextView.setText("圈子成员");
        } else {
            detaiTextView.setText("班级详情");
            menberTextView.setText("班级成员");
        }

    }

    @Override
    protected void initLocalData() {
        Intent intent = getIntent();
        if (intent != null){
            currentClasses =  (Classes)intent.getSerializableExtra("classes");
            classDetail  =  (ClassDetail)intent.getSerializableExtra("classDetail");
        }
        if(classDetail != null){
            updateView();
        } else {
            loadClassDetail();
        }
    }


    @Override
    protected void initListener() {
        findViewById(R.id.btn_class_detail).setOnClickListener(this);
        findViewById(R.id.btn_classe_menber).setOnClickListener(this);
        findViewById(R.id.btn_feedback_content).setOnClickListener(this);
        findViewById(R.id.btn_push_advice).setOnClickListener(this);
        findViewById(R.id.btn_share_class).setOnClickListener(this);
        findViewById(R.id.btn_notification_view).setOnClickListener(this);
        shareSwitch.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_class_detail:
                if (currentClasses != null && classDetail != null) {
                    if (currentClasses.isTeacherFriendCircle()) {
                        WebParcel parcel = new WebParcel(); //组织圈
                        parcel.setTitle("圈子详情");
                        String url = RuiXConstant.WX_TEACHE_FREAND_CIRECLE_SHARE + classDetail.getEncryptId();
                        //&source=android&is_show_bottombar=0
                        parcel.setUrl(url);
                        parcel.setClassDetail(true);
                        startActivity(CommonWebActivity.newIntent(this, parcel));
                    } else {
                        startActivity(ClassInfoActivity.newIntent(this, currentClasses, classDetail));
                    }
                }
                break;
            case R.id.btn_classe_menber:
                startActivity(ClassMenberActivity.newIntent(this,currentClasses,classDetail));
                break;
            case R.id.btn_feedback_content:
                startActivity(FeadbackAdviceActivity.newIntent(this,currentClasses));
                break;
            case R.id.btn_push_advice:
                startActivity(PushAdviceActivity.newIntent(this,currentClasses));
                break;
            case R.id.btn_share_class:
                if (currentClasses != null && classDetail != null) {
                    startActivity(ClassShareActivity.newIntent(this, currentClasses, classDetail));
                }
                break;

            case R.id.btn_notification_view:
                startActivity(ClassStatusActivity.newIntent(this,currentClasses,classDetail));
                break;
        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {
        if (currentClasses == null){
            return;
        }

        showLoading();
        RequestFactory.setShareState(currentClasses.getID(), isChecked, null, new RXObserver() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (classDetail != null) {
                    classDetail.setCanSharePost(isChecked);
                }
                sendShareStateChane(isChecked);
            }

            @Override
            public void onError(Throwable e) {
                dismissLoading();
                resolveException(e);
                shareSwitch.setChecked(!isChecked,false);
            }

            @Override
            public void onComplete() {
                dismissLoading();
            }
        });

    }



    private void loadClassDetail(){
        if (currentClasses == null){
            return;
        }
        showLoading();
        RequestFactory.classDetail(currentClasses.getID(), new ClasssDetailMapper(currentClasses.getID()), new RXObserver<ClassDetail>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ClassDetail value) {
                if (value != null) {
                    classDetail = value;
                    updateView();
                }
            }

            @Override
            public void onError(Throwable e) {
                dismissLoading();
                resolveException(e);
            }

            @Override
            public void onComplete() {
                dismissLoading();

            }
        });

    }


    private void sendShareStateChane( boolean isChecked){
        Intent intent = new Intent();
        intent.putExtra("canshare",isChecked);
        intent.setAction(RuiXConstant.NOTICE_CHANGE_SHARE_STATE);//RuiXConstant.NOTICE_CHANGE_SHARE_STATE
        sendOrderedBroadcast(intent,null);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
